//
//  NSUserDefaults.m
//  BaseFramework
//
//  Created by hnb on 2016/11/17.
//  Copyright © 2016年 yncc. All rights reserved.
//

#import "UserDefaults.h"

@implementation UserDefaults
//读取数据
+(id)readUserDefaultObjectValueForKey:(NSString*)aKey {
    if (aKey) {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSData *data = [defaults objectForKey:aKey];
        return [NSKeyedUnarchiver unarchiveObjectWithData:data];
    }
    else {
        return nil;
    }
}
//写入数据
+(void)writeUserDefaultObjectValue:(NSObject*)aValue
                           withKey:(NSString*)aKey {
    if (!aValue || !aKey)
    {
        return;
    }
    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSObject *objc = aValue ; // set value
    NSData *data = [NSKeyedArchiver archivedDataWithRootObject:objc];
    [defaults setObject:data forKey:aKey];
    [defaults synchronize];
}
//清除数据
+ (void)clearUserDefaultWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
//判断包含数据
+(BOOL)hasUserDefaultWithKey:(NSString *)key
{
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] dictionaryRepresentation];
    return [[dic allKeys] containsObject:key];
}
@end
